<?php


namespace DebugBar\DataCollector;

use ArrayAccess;
use DebugBar\DebugBarException;


class AggregatedCollector implements DataCollectorInterface, ArrayAccess
{
    protected $name;

    protected $mergeProperty;

    protected $sort;

    protected $collectors = array();

    
    public function __construct($name, $mergeProperty = null, $sort = false)
    {
        $this->name = $name;
        $this->mergeProperty = $mergeProperty;
        $this->sort = $sort;
    }

    
    public function addCollector(DataCollectorInterface $collector)
    {
        $this->collectors[$collector->getName()] = $collector;
    }

    
    public function getCollectors()
    {
        return $this->collectors;
    }

    
    public function setMergeProperty($property)
    {
        $this->mergeProperty = $property;
    }

    
    public function getMergeProperty()
    {
        return $this->mergeProperty;
    }

    
    public function setSort($sort)
    {
        $this->sort = $sort;
    }

    
    public function getSort()
    {
        return $this->sort;
    }

    
    public function collect()
    {
        $aggregate = array();
        foreach ($this->collectors as $collector) {
            $data = $collector->collect();
            if ($this->mergeProperty !== null) {
                $data = $data[$this->mergeProperty];
            }
            $aggregate = array_merge($aggregate, $data);
        }

        return $this->sort($aggregate);
    }

    
    protected function sort($data)
    {
        if (is_string($this->sort)) {
            $p = $this->sort;
            usort($data, function ($a, $b) use ($p) {
                if ($a[$p] == $b[$p]) {
                    return 0;
                }
                return $a[$p] < $b[$p] ? -1 : 1;
            });
        } elseif ($this->sort === true) {
            sort($data);
        }
        return $data;
    }

    
    public function getName()
    {
        return $this->name;
    }

        
    
    public function offsetSet($key, $value)
    {
        throw new DebugBarException("AggregatedCollector[] is read-only");
    }

    
    public function offsetGet($key)
    {
        return $this->collectors[$key];
    }

    
    public function offsetExists($key)
    {
        return isset($this->collectors[$key]);
    }

    
    public function offsetUnset($key)
    {
        throw new DebugBarException("AggregatedCollector[] is read-only");
    }
}
